package com.example.gkcircle.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.AbsListView;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ListView;

import com.camnter.easyslidingtabs.widget.EasySlidingTabs;
import com.example.gkcircle.App;
import com.example.gkcircle.BaseActivity;
import com.example.gkcircle.BaseAppCompatActivity;
import com.example.gkcircle.R;
import com.example.gkcircle.adapter.TabsFragmentAdapter;
import com.example.gkcircle.fragment.lost.FoundFragment;
import com.example.gkcircle.fragment.lost.LostFragment;
import com.example.gkcircle.utils.ActivityUtil;
import com.example.gkcircle.utils.PicUtil;
import com.example.gkcircle.utils.ToastFactory;
import com.oguzdev.circularfloatingactionmenu.library.FloatingActionButton;
import com.oguzdev.circularfloatingactionmenu.library.FloatingActionMenu;
import com.oguzdev.circularfloatingactionmenu.library.SubActionButton;

import java.util.LinkedList;
import java.util.List;

/**
 * 失物招领
 * Created by Jehu on 16/3/18.
 */
public class LostAndFoundActivity extends BaseAppCompatActivity implements View.OnClickListener {


    private EasySlidingTabs easySlidingTabs;
    private ViewPager easyVP;
    private TabsFragmentAdapter adapter;
    private ImageButton mBack;
    private ImageButton mUserInfo;
    List<Fragment> fragments;
    public static final String[] titles = {"失物", "招领"};
    private final int LOST_CODE=0;
    private final int FOUND_CODE=1;

    //浮动菜单
    public static int firstVisibleItem;
    public static FloatingActionMenu mFloatingActionMenu;
    public static FloatingActionButton mFloatingActionButton;
    public static SubActionButton mSubActionButtonLost;
    public static SubActionButton mSubActionButtonFound;


    @Override
    protected void initView() {
        setContentView(R.layout.home_lost_found_activity);
        this.easySlidingTabs = (EasySlidingTabs) this.findViewById(R.id.lost_found_easy_sliding_tabs);
        this.easyVP = (ViewPager) this.findViewById(R.id.lost_found_easy_vp);
        this.mBack = (ImageButton) this.findViewById(R.id.lost_back);
        this.mUserInfo = (ImageButton) this.findViewById(R.id.lost_user_info);
    }

    @Override
    protected void initListener() {
        mBack.setOnClickListener(this);
    }

    @Override
    protected void setData() {
        this.fragments = new LinkedList<>();
        LostFragment lost = LostFragment.getInstance();
        FoundFragment found = FoundFragment.getInstance();
        this.fragments.add(lost);
        this.fragments.add(found);
        this.adapter = new TabsFragmentAdapter(this.getSupportFragmentManager(), titles, this.fragments);
        this.easyVP.setAdapter(this.adapter);
        this.easySlidingTabs.setViewPager(this.easyVP);

        //设置浮动菜单
        setFloatingMenu();

    }


    private void setFloatingMenu() {
        //创建发布图标
        ImageView icon = new ImageView(this);
        icon.setImageDrawable(getResources().getDrawable(R.mipmap.lost_compose));

        //创建浮动发布按钮
        mFloatingActionButton = new FloatingActionButton.Builder(this).setContentView(icon).build();

        //创建失物子浮动按钮
        SubActionButton.Builder itemBuilder = new SubActionButton.Builder(this);

        ImageView lostItem = new ImageView(this);
        lostItem.setImageDrawable(getResources().getDrawable(R.mipmap.lost_compose_menu));

        //创建招领子浮动按钮
        ImageView foundItem = new ImageView(this);
        foundItem.setImageDrawable(getResources().getDrawable(R.mipmap.found_compose_menu));

        mSubActionButtonLost = itemBuilder.setContentView(lostItem).build();
        mSubActionButtonFound = itemBuilder.setContentView(foundItem).build();

        mSubActionButtonLost.setLayoutParams(new AbsListView.LayoutParams(PicUtil.dip2px(this, 60f), PicUtil.dip2px(this, 60f)));
        mSubActionButtonFound.setLayoutParams(new AbsListView.LayoutParams(PicUtil.dip2px(this, 60f), PicUtil.dip2px(this, 60f)));


        mFloatingActionMenu = new FloatingActionMenu.Builder(this)
                .addSubActionView(mSubActionButtonLost)
                .addSubActionView(mSubActionButtonFound)
                .attachTo(mFloatingActionButton)
                .build();


        //失物菜单点击事件
        mSubActionButtonLost.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Intent intent=new Intent(LostAndFoundActivity.this,ComposeLostActivity.class);
                Bundle bundle=new Bundle();
                bundle.putInt("flag", 0);
                intent.putExtras(bundle);
                startActivityForResult(intent,LOST_CODE);
            }
        });
        //招领菜单点击事件
        mSubActionButtonFound.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(LostAndFoundActivity.this,ComposeLostActivity.class);
                Bundle bundle=new Bundle();
                bundle.putInt("flag",1);
                intent.putExtras(bundle);
                startActivityForResult(intent, FOUND_CODE);
            }
        });
    }


    @Override
    public void onClick(View v) {

        switch (v.getId()) {
            case R.id.lost_back:
                this.finish();
                break;
            default:
                break;
        }

    }



    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(resultCode==RESULT_OK){
            switch (requestCode){
                case LOST_CODE:
                    LostFragment.iActivityResultListener.complete();
                    break;
                case FOUND_CODE:
                    FoundFragment.iActivityResultListener.complete();
                    break;
            }
        }
    }
}
